package com.jse.oo.design.pattern.structural.adaptor;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.junit.Test;


/**
 * 有两种适配器：
 * 1、实现接口
 * 2、包含特定的对象
 * @author zenghua233
 *
 */
public class IOAdaptorTest {

	/**
	 * 测试对象类型的适配器
	 */
	public void testObject() throws Exception {
		//FileInputStream实现了InputStream接口，但是里面还有的FileDescriptor读取文件功能
		InputStream in = new FileInputStream(new File(""));
		
		
		byte[] bytes = new byte[1024];
		//
		InputStream bytein = new ByteArrayInputStream("hello".getBytes("UTF-8"));
	}
	
	/**
	 * stream到char的转换
	 */
	@Test
	public void testObject1() throws Exception {
		InputStream in = new FileInputStream(new File("F:\\opensource\\oschina\\re-javase\\src\\main\\java\\com\\jse\\oo\\design\\pattern\\structural\\adaptor\\IOAdaptorTest.java"));
		//InputStreamReader本来是Reader类型的，要实现InputStream功能，只有传入InputStream
		InputStreamReader reader = new InputStreamReader(in, "UTF-8");
		int i;
		//直接读取char
//		while((i = reader.read()) != -1) {
//			char c = (char) i;
//			System.out.print(c);
//		}
		
		//缓存字符
		BufferedReader bfReader = new BufferedReader(reader);
		String s;
		while((s = bfReader.readLine()) != null) {
			System.out.println(s);
		}
		
		bfReader.close();
		reader.close();
		in.close();
	}
	
	/**
	 * 测试实现接口类型的适配器
	 * @throws Exception
	 */
	public void testInterface() throws Exception {
		
	}
}
